import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import com.tinkerforge.BrickIMU;
import com.tinkerforge.BrickIMU.AccelerationListener;
import com.tinkerforge.BrickIMU.AngularVelocityListener;
import com.tinkerforge.BrickIMU.MagneticFieldListener;
import com.tinkerforge.BrickIMU.OrientationListener;
import com.tinkerforge.Device;
import com.tinkerforge.TinkerforgeException;
import fbi.mobiComp.integrator.implementation.AdaptiveIntegrator;
import bfh.fbi.mobiComp.integrator.definition.*;

import ch.bfh.earthquake.*;

import ch.quantasy.communication.channel.definition.ChannelManager;
import ch.quantasy.communication.channel.definition.MessageReceiver;
import ch.quantasy.communication.channel.definition.MessageReceiverListener;
import ch.quantasy.communication.channel.implementation.DefaultChannelManager;
import ch.quantasy.communication.channel.implementation.udp.UDPBroadcastChannel;
import ch.quantasy.communication.chat.definition.Chat;
import ch.quantasy.communication.chat.definition.ChatMessage;
import ch.quantasy.communication.chat.definition.ChatMessageManager;
import ch.quantasy.communication.chat.implementation.MyChatMessage;
import ch.quantasy.communication.chat.implementation.MyChatMessageManager;

public class DetectorSensor extends TinkerManager implements Chat{

	public static final String EARTQUAKE_ACTOR_HOST_NAME = "localhost"; //"192.168.43.13";

	private final AccelerationListener accelerationListener;
	private final MagneticFieldListener magneticListener;
	private final AngularVelocityListener angularListener;
	private final OrientationListener orientationListener;
	private static PeakListener peakListener;
	private static AdaptiveIntegrator integrator;

	private ChatMessageManager chatMessageManager;
	private DetectorSettingsManager setMngr;
	private DetectorSettings detSet;



	public DetectorSensor(final AccelerationListener accelerationListener,
							final MagneticFieldListener magneticListener,
							final AngularVelocityListener angularListener,
							final OrientationListener orientationListener) {

		super(EARTQUAKE_ACTOR_HOST_NAME);

		this.accelerationListener = accelerationListener;
		this.magneticListener = magneticListener;
		this.angularListener = angularListener;
		this.orientationListener = orientationListener;

	peakListener = new PeakListener() {

			@Override
			public void onPeak(double peak, double average) {
				//System.out.println("peak : " + peak);

			}
		};

		try {
			integrator = new AdaptiveIntegrator(200, 1000, peakListener);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	void init() {
		
		this.detSet = DetectorSettings.getInstance();
		this.setMngr = new DetectorSettingsManager();
		this.chatMessageManager = new MyChatMessageManager(this);

		while(true){
			//System.out.println("Average : "+integrator.getAverage());

			if (detSet.isEnableMovingSensor()){
				double quake = integrator.getRoundedAverage();
				if(quake == 0.0){

					//System.out.println("Earthquake");
					String id = "" + System.currentTimeMillis() % 100000;
					chatMessageManager.send(new MyChatMessage("siberia", "Earthquake : Timestamp:" + id));
				}
				else
				{
					//System.out.println("Earth moving: " + integrator.getAverage() + " Quake: " + quake);
				}
			}
		}
	}

	@Override
	public void connected() {
		// TODO Auto-generated method stub
	}

	@Override
	public void disconnected() {
		// TODO Auto-generated method stub

	}

	@Override
	public void deviceConnected(Device device) {
		this.setListener(device);
	}

	@Override
	public void deviceReConnected(Device device) {
		this.setListener(device);
	}

	@Override
	public void deviceDisconnected(Device device) {
		// TODO Auto-generated method stub

	}

	public static void setAngular(short x, short y, short z) {
		//System.out.println("Angular:: x: " + x +" y: " + z + " z: " + z);
	}

	public static void setMagField(short x, short y, short z) {
		//System.out.println("Magnetic field:: x: " + x +" y: " + z + " z: " + z);
	}

	public static void setOrientation(short roll, short pitch, short yaw) {
		//System.out.println("Orientation:: Roll: " + roll +" ptich: " + pitch + " yaw: " + yaw);
	}

	public static void setAcceleration(short x, short y, short z) {

		integrator.addSample(z);

		//System.out.println("Earth is shaking... " + z);
	}

	public static void setTemp(short temperature) {
		//System.out.println("Temprature: " + temperature);
	}

	private void setListener(final Device device) {
		if (TinkerforgeDevice.getDevice(device) == TinkerforgeDevice.IMU) {
			final BrickIMU imu = (BrickIMU) device;
			//ambient.addIlluminanceListener(this.illuminanceListener);
			imu.addAccelerationListener(this.accelerationListener);
			imu.addAngularVelocityListener(this.angularListener);
			imu.addOrientationListener(this.orientationListener);
			imu.addMagneticFieldListener(this.magneticListener);
			try{
				//imu.setAllDataPeriod(500);
				imu.setAccelerationPeriod(20);
				imu.setAngularVelocityPeriod(500);
				imu.setOrientationPeriod(500);
				imu.setMagneticFieldPeriod(500);

			} catch (final TinkerforgeException ex) {
			}
		}
	}

	@Override
	public void addMessage(ChatMessage chatMessage) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setMessageDelivered() {
		// TODO Auto-generated method stub

	}



}
